/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: MenuView.java 68 2011-08-01 22:22:17Z jchimene@gmail.com $
 */
package com.systasis.sked.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.systasis.sked.client.event.ResetEvent;
import com.systasis.sked.client.event.RestoreEvent;
import com.systasis.sked.client.event.SaveEvent;
import com.systasis.sked.client.presenter.AboutPresenter;
import com.systasis.sked.client.presenter.HelpPresenter;
import com.systasis.sked.client.presenter.MenuPresenter;
import com.systasis.sked.client.presenter.StoragePresenter;
import com.systasis.sked.client.resources.SkedStrings;

@Singleton
public class MenuView implements MenuPresenter.MyView {

    private AboutPresenter aboutPresenter;
    final private Provider<AboutPresenter> aboutProvider;
    final private ActivityView activityView;
    final private SkedStrings CONSTANTS = GWT.create(SkedStrings.class);
    final private EventBus eventBus;
    private HelpPresenter helpPresenter;
    final private Provider<HelpPresenter> helpProvider;
    final private MenuBar mainMenu;
    private StoragePresenter storagePresenter;

    final private Provider<StoragePresenter> storageProvider;

    @Inject
    public MenuView(EventBus eventBus //
	    , ActivityView activityView //
	    , Provider<HelpPresenter> helpProvider //
	    , Provider<AboutPresenter> aboutProvider //
	    , Provider<StoragePresenter> storageProvider) {
	this.activityView = activityView;
	this.eventBus = eventBus;
	this.helpProvider = helpProvider;
	this.aboutProvider = aboutProvider;
	this.storageProvider = storageProvider;
	mainMenu = new MenuBar();
    }

    @Override
    public void addToSlot(Object slot, Widget content) {
    }

    @Override
    public Widget asWidget() {
	return mainMenu;
    }

    @Override
    public void removeFromSlot(Object slot, Widget content) {
    }

    @Override
    public void setInSlot(Object slot, Widget content) {
    }

    @Override
    public void startProcessing() {
	final MenuBar helpMenu = new MenuBar(true); // create Vertical menubar
	final MenuBar fileMenu = new MenuBar(true); // create Vertical menubar

	helpMenu.addItem(new MenuItem(CONSTANTS.titleHelp(), new Command() {
	    @Override
	    public void execute() {
		if (null == helpPresenter) {
		    helpPresenter = helpProvider.get();
		}
		helpPresenter.helpCallback();
	    }
	}));

	helpMenu.addItem(new MenuItem(CONSTANTS.titleAbout(), new Command() {
	    @Override
	    public void execute() {
		if (null == aboutPresenter) {
		    aboutPresenter = aboutProvider.get();
		}
		aboutPresenter.aboutCallback();
	    }
	}));

	fileMenu.addItem(CONSTANTS.titleNewSchedule(), new Command() {
	    @Override
	    public void execute() {
		eventBus.fireEvent(new ResetEvent());
	    }
	});

	fileMenu.addItem(CONSTANTS.titleLoadPreviousSchedule(), new Command() {
	    @Override
	    public void execute() {
		if (null == storagePresenter) {
		    storagePresenter = storageProvider.get();
		}
		eventBus.fireEvent(new RestoreEvent());
	    }
	});

	fileMenu.addItem(CONSTANTS.titleSaveSchedule(), new Command() {

	    @Override
	    public void execute() {
		if (null == storagePresenter) {
		    storagePresenter = storageProvider.get();
		}
		eventBus.fireEvent(new SaveEvent());
	    }
	});

	mainMenu.addItem(CONSTANTS.titleFile(), false, fileMenu);
	mainMenu.addItem(CONSTANTS.titleHelp(), false, helpMenu);
	activityView.getMenu().add(mainMenu);
    }

}
